<template>
  <div class="app-container">
    <a-title @clickPreview="preview" />
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="100px">
      <el-form-item label="业务手册名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入业务手册名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      stripe
      v-loading="loading"
      :data="guideList"
      @sort-change="tableSortChange"
      :default-sort="{prop: 'fileUpdateTime', order: 'descending'}"
    >
      <el-table-column
        label="业务手册名称"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
        align="center"
        prop="title"
      />
      <el-table-column
        label="更新时间"
        align="center"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
        prop="fileUpdateTime"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fileUpdateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="文件大小"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
        align="center"
        prop="contentSize"
      />
      <el-table-column
        label="业务手册文件"
        :sort-orders="['ascending', 'descending']"
        sortable="custom"
        align="center"
        prop="fileName"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.fileName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handlePreview(scope.row)"
          >预览</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 预览 -->
    <el-dialog :visible.sync="openPreview" width="80%">
      <iframe :src="manualPreviewUrl" frameborder="0" width="100%" height="700px"></iframe>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGuide,
  getGuide,
  delGuide,
  addGuide,
  updateGuide,
  exportGuide
} from "@/api/businessStandardLibrary/guide/guide";
import aTitle from "@/components/Title";
export default {
  name: "Guide",
  components: { aTitle },
  data () {
    return {
      manualPreviewUrl: null,
      openPreview: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 日期范围
      dateRange: [],
      // 业务指南表格数据
      guideList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        businessCategoryId: undefined,
        sortBy: undefined,
        order: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created () {
    this.queryParams.businessCategoryId = this.$route.params.businessCategoryId;
    this.getList();
  },
  mounted(){
    document.title=document.getElementById("businessNameTitle").innerHTML;
  },
  methods: {
    preview () {
      this.openPreview = true;
      this.manualPreviewUrl = `${this.previewUrl}${encodeURIComponent(
        `${this.operationManual}业务主页/业务手册.xlsx`
      )}&name=${encodeURIComponent("业务手册.xlsx")}`;
    },
    /** 查询业务指南列表 */
    getList () {
      this.loading = true;
      listGuide(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.guideList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = []
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids;
      getGuide(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改业务指南";
      });
    },
    handlePreview (row) {
      this.filePreview(row.fileName, row.storagePath);
    },
    tableSortChange (column) {
      this.queryParams.pageNum = 1;

      if (column.order === "descending") {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "desc";
      } else {
        this.queryParams.sortBy = column.prop;
        this.queryParams.order = "asc";
      }
      this.getList();
    }
  }
};
</script>
